package com.test.mooan.labama.bean_boom.product;

import com.test.mooan.labama.constants_boom.Constant_boom;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

public class ProductListInfo {

    private static ProductListInfo instance;

    private ProductListInfo(){}

    public static ProductListInfo gtInstance(){
        if (instance == null) return new ProductListInfo();
        return instance;
    }

    public static void stInstance(JSONArray jsonArray){

        try {

            if (instance == null) instance = new ProductListInfo();
            else {
                instance.productList.clear();
                instance.repayProductList.clear();
            }

            if (jsonArray!=null){
                for (int i=0;i<jsonArray.length();i++){
                    JSONObject jsonObject = jsonArray.optJSONObject(i);
                    if (jsonObject!=null) {
                        Product product = Product.gtInstance(jsonObject);
                        switch (product.productStatus) {
                            case Order_Pending_Repayment, Order_Overdue_Repayment ->
                                    instance.repayProductList.add(product);
                            default -> instance.productList.add(product);
                        }
                    }
                }

            }

        }catch (Exception e){
            if (Constant_boom.Debug) e.printStackTrace();
        }
    }

    private final ArrayList<Product> productList = new ArrayList<>();
    private final ArrayList<Product> repayProductList = new ArrayList<>();

    public ArrayList<Product> gtProductList() {
        return new ArrayList<>(productList);
    }

    public ArrayList<Product> gtRepayProductList() {
        return new ArrayList<>(repayProductList);
    }

}
